home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PEAR / Frontend / CLI.php
PHP Script  |  2004-10-01  |  17KB  |  510 lines

  1. <?php
  2. /*
  3.   +----------------------------------------------------------------------+
  4.   | PHP Version 5                                                        |
  5.   +----------------------------------------------------------------------+
  6.   | Copyright (c) 1997-2004 The PHP Group                                |
  7.   +----------------------------------------------------------------------+
  8.   | This source file is subject to version 3.0 of the PHP license,       |
  9.   | that is bundled with this package in the file LICENSE, and is        |
  10.   | available through the world-wide-web at the following url:           |
  11.   | http://www.php.net/license/3_0.txt.                                  |
  12.   | If you did not receive a copy of the PHP license and are unable to   |
  13.   | obtain it through the world-wide-web, please send a note to          |
  14.   | license@php.net so we can mail you a copy immediately.               |
  15.   +----------------------------------------------------------------------+
  16.   | Author: Stig Sµther Bakken <ssb@php.net>                             |
  17.   +----------------------------------------------------------------------+
  18.  
  19.   $Id: CLI.php,v 1.41 2004/02/17 05:49:16 ssb Exp $
  20. */
  21.  
  22. require_once "PEAR.php";
  23.  
  24. class PEAR_Frontend_CLI extends PEAR
  25. {
  26.     // {{{ properties
  27.  
  28.     /**
  29.      * What type of user interface this frontend is for.
  30.      * @var string
  31.      * @access public
  32.      */
  33.     var $type = 'CLI';
  34.     var $lp = ''; // line prefix
  35.  
  36.     var $params = array();
  37.     var $term = array(
  38.         'bold' => '',
  39.         'normal' => '',
  40.         );
  41.  
  42.     // }}}
  43.  
  44.     // {{{ constructor
  45.  
  46.     function PEAR_Frontend_CLI()
  47.     {
  48.         parent::PEAR();
  49.         $term = getenv('TERM'); //(cox) $_ENV is empty for me in 4.1.1
  50.         if (function_exists('posix_isatty') && !posix_isatty(1)) {
  51.             // output is being redirected to a file or through a pipe
  52.         } elseif ($term) {
  53.             // XXX can use ncurses extension here, if available
  54.             if (preg_match('/^(xterm|vt220|linux)/', $term)) {
  55.                 $this->term['bold'] = sprintf("%c%c%c%c", 27, 91, 49, 109);
  56.                 $this->term['normal']=sprintf("%c%c%c", 27, 91, 109);
  57.             } elseif (preg_match('/^vt100/', $term)) {
  58.                 $this->term['bold'] = sprintf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0);
  59.                 $this->term['normal']=sprintf("%c%c%c%c%c", 27, 91, 109, 0, 0);
  60.             }
  61.         } elseif (OS_WINDOWS) {
  62.             // XXX add ANSI codes here
  63.         }
  64.     }
  65.  
  66.     // }}}
  67.  
  68.     // {{{ displayLine(text)
  69.  
  70.     function displayLine($text)
  71.     {
  72.         trigger_error("PEAR_Frontend_CLI::displayLine deprecated", E_USER_ERROR);
  73.     }
  74.  
  75.     function _displayLine($text)
  76.     {
  77.         print "$this->lp$text\n";
  78.     }
  79.  
  80.     // }}}
  81.     // {{{ display(text)
  82.  
  83.     function display($text)
  84.     {
  85.         trigger_error("PEAR_Frontend_CLI::display deprecated", E_USER_ERROR);
  86.     }
  87.  
  88.     function _display($text)
  89.     {
  90.         print $text;
  91.     }
  92.  
  93.     // }}}
  94.     // {{{ displayError(eobj)
  95.  
  96.     /**
  97.      * @param object PEAR_Error object
  98.      */
  99.     function displayError($eobj)
  100.     {
  101.         return $this->_displayLine($eobj->getMessage());
  102.     }
  103.  
  104.     // }}}
  105.     // {{{ displayFatalError(eobj)
  106.  
  107.     /**
  108.      * @param object PEAR_Error object
  109.      */
  110.     function displayFatalError($eobj)
  111.     {
  112.         $this->displayError($eobj);
  113.         exit(1);
  114.     }
  115.  
  116.     // }}}
  117.     // {{{ displayHeading(title)
  118.  
  119.     function displayHeading($title)
  120.     {
  121.         trigger_error("PEAR_Frontend_CLI::displayHeading deprecated", E_USER_ERROR);
  122.     }
  123.  
  124.     function _displayHeading($title)
  125.     {
  126.         print $this->lp.$this->bold($title)."\n";
  127.         print $this->lp.str_repeat("=", strlen($title))."\n";
  128.     }
  129.  
  130.     // }}}
  131.     // {{{ userDialog(prompt, [type], [default])
  132.  
  133.     function userDialog($command, $prompts, $types = array(), $defaults = array())
  134.     {
  135.         $result = array();
  136.         if (is_array($prompts)) {
  137.             $fp = fopen("php://stdin", "r");
  138.             foreach ($prompts as $key => $prompt) {
  139.                 $type = $types[$key];
  140.                 $default = @$defaults[$key];
  141.                 if ($type == 'password') {
  142.                     system('stty -echo');
  143.                 }
  144.                 print "$this->lp$prompt ";
  145.                 if ($default) {
  146.                     print "[$default] ";
  147.                 }
  148.                 print ": ";
  149.                 $line = fgets($fp, 2048);
  150.                 if ($type == 'password') {
  151.                     system('stty echo');
  152.                     print "\n";
  153.                 }
  154.                 if ($default && trim($line) == "") {
  155.                     $result[$key] = $default;
  156.                 } else {
  157.                     $result[$key] = $line;
  158.                 }
  159.             }
  160.             fclose($fp);
  161.         }
  162.         return $result;
  163.     }
  164.  
  165.     // }}}
  166.     // {{{ userConfirm(prompt, [default])
  167.  
  168.     function userConfirm($prompt, $default = 'yes')
  169.     {
  170.         trigger_error("PEAR_Frontend_CLI::userConfirm not yet converted", E_USER_ERROR);
  171.         static $positives = array('y', 'yes', 'on', '1');
  172.         static $negatives = array('n', 'no', 'off', '0');
  173.         print "$this->lp$prompt [$default] : ";
  174.         $fp = fopen("php://stdin", "r");
  175.         $line = fgets($fp, 2048);
  176.         fclose($fp);
  177.         $answer = strtolower(trim($line));
  178.         if (empty($answer)) {
  179.             $answer = $default;
  180.         }
  181.         if (in_array($answer, $positives)) {
  182.             return true;
  183.         }
  184.         if (in_array($answer, $negatives)) {
  185.             return false;
  186.         }
  187.         if (in_array($default, $positives)) {
  188.             return true;
  189.         }
  190.         return false;
  191.     }
  192.  
  193.     // }}}
  194.     // {{{ startTable([params])
  195.  
  196.     function startTable($params = array())
  197.     {
  198.         trigger_error("PEAR_Frontend_CLI::startTable deprecated", E_USER_ERROR);
  199.     }
  200.  
  201.     function _startTable($params = array())
  202.     {
  203.         $params['table_data'] = array();
  204.         $params['widest'] = array();  // indexed by column
  205.         $params['highest'] = array(); // indexed by row
  206.         $params['ncols'] = 0;
  207.         $this->params = $params;
  208.     }
  209.  
  210.     // }}}
  211.     // {{{ tableRow(columns, [rowparams], [colparams])
  212.  
  213.     function tableRow($columns, $rowparams = array(), $colparams = array())
  214.     {
  215.         trigger_error("PEAR_Frontend_CLI::tableRow deprecated", E_USER_ERROR);
  216.     }
  217.  
  218.     function _tableRow($columns, $rowparams = array(), $colparams = array())
  219.     {
  220.         $highest = 1;
  221.         for ($i = 0; $i < sizeof($columns); $i++) {
  222.             $col = &$columns[$i];
  223.             if (isset($colparams[$i]) && !empty($colparams[$i]['wrap'])) {
  224.                 $col = wordwrap($col, $colparams[$i]['wrap'], "\n", 0);
  225.             }
  226.             if (strpos($col, "\n") !== false) {
  227.                 $multiline = explode("\n", $col);
  228.                 $w = 0;
  229.                 foreach ($multiline as $n => $line) {
  230.                     if (strlen($line) > $w) {
  231.                         $w = strlen($line);
  232.                     }
  233.                 }
  234.                 $lines = sizeof($multiline);
  235.             } else {
  236.                 $w = strlen($col);
  237.             }
  238.             if ($w > @$this->params['widest'][$i]) {
  239.                 $this->params['widest'][$i] = $w;
  240.             }
  241.             $tmp = count_chars($columns[$i], 1);
  242.             // handle unix, mac and windows formats
  243.             $lines = (isset($tmp[10]) ? $tmp[10] : @$tmp[13]) + 1;
  244.             if ($lines > $highest) {
  245.                 $highest = $lines;
  246.             }
  247.         }
  248.         if (sizeof($columns) > $this->params['ncols']) {
  249.             $this->params['ncols'] = sizeof($columns);
  250.         }
  251.         $new_row = array(
  252.             'data' => $columns,
  253.             'height' => $highest,
  254.             'rowparams' => $rowparams,
  255.             'colparams' => $colparams,
  256.             );
  257.         $this->params['table_data'][] = $new_row;
  258.     }
  259.  
  260.     // }}}
  261.     // {{{ endTable()
  262.  
  263.     function endTable()
  264.     {
  265.         trigger_error("PEAR_Frontend_CLI::endTable deprecated", E_USER_ERROR);
  266.     }
  267.  
  268.     function _endTable()
  269.     {
  270.         extract($this->params);
  271.         if (!empty($caption)) {
  272.             $this->_displayHeading($caption);
  273.         }
  274.         if (count($table_data) == 0) {
  275.             return;
  276.         }
  277.         if (!isset($width)) {
  278.             $width = $widest;
  279.         } else {
  280.             for ($i = 0; $i < $ncols; $i++) {
  281.                 if (!isset($width[$i])) {
  282.                     $width[$i] = $widest[$i];
  283.                 }
  284.             }
  285.         }
  286.         $border = false;
  287.         if (empty($border)) {
  288.             $cellstart = '';
  289.             $cellend = ' ';
  290.             $rowend = '';
  291.             $padrowend = false;
  292.             $borderline = '';
  293.         } else {
  294.             $cellstart = '| ';
  295.             $cellend = ' ';
  296.             $rowend = '|';
  297.             $padrowend = true;
  298.             $borderline = '+';
  299.             foreach ($width as $w) {
  300.                 $borderline .= str_repeat('-', $w + strlen($cellstart) + strlen($cellend) - 1);
  301.                 $borderline .= '+';
  302.             }
  303.         }
  304.         if ($borderline) {
  305.             $this->_displayLine($borderline);
  306.         }
  307.         for ($i = 0; $i < sizeof($table_data); $i++) {
  308.             extract($table_data[$i]);
  309.             if (!is_array($rowparams)) {
  310.                 $rowparams = array();
  311.             }
  312.             if (!is_array($colparams)) {
  313.                 $colparams = array();
  314.             }
  315.             $rowlines = array();
  316.             if ($height > 1) {
  317.                 for ($c = 0; $c < sizeof($data); $c++) {
  318.                     $rowlines[$c] = preg_split('/(\r?\n|\r)/', $data[$c]);
  319.                     if (sizeof($rowlines[$c]) < $height) {
  320.                         $rowlines[$c] = array_pad($rowlines[$c], $height, '');
  321.                     }
  322.                 }
  323.             } else {
  324.                 for ($c = 0; $c < sizeof($data); $c++) {
  325.                     $rowlines[$c] = array($data[$c]);
  326.                 }
  327.             }
  328.             for ($r = 0; $r < $height; $r++) {
  329.                 $rowtext = '';
  330.                 for ($c = 0; $c < sizeof($data); $c++) {
  331.                     if (isset($colparams[$c])) {
  332.                         $attribs = array_merge($rowparams, $colparams);
  333.                     } else {
  334.                         $attribs = $rowparams;
  335.                     }
  336.                     $w = isset($width[$c]) ? $width[$c] : 0;
  337.                     //$cell = $data[$c];
  338.                     $cell = $rowlines[$c][$r];
  339.                     $l = strlen($cell);
  340.                     if ($l > $w) {
  341.                         $cell = substr($cell, 0, $w);
  342.                     }
  343.                     if (isset($attribs['bold'])) {
  344.                         $cell = $this->bold($cell);
  345.                     }
  346.                     if ($l < $w) {
  347.                         // not using str_pad here because we may
  348.                         // add bold escape characters to $cell
  349.                         $cell .= str_repeat(' ', $w - $l);
  350.                     }
  351.  
  352.                     $rowtext .= $cellstart . $cell . $cellend;
  353.                 }
  354.                 if (!$border) {
  355.                     $rowtext = rtrim($rowtext);
  356.                 }
  357.                 $rowtext .= $rowend;
  358.                 $this->_displayLine($rowtext);
  359.             }
  360.         }
  361.         if ($borderline) {
  362.             $this->_displayLine($borderline);
  363.         }
  364.     }
  365.  
  366.     // }}}
  367.     // {{{ outputData()
  368.  
  369.     function outputData($data, $command = '_default')
  370.     {
  371.         switch ($command) {
  372.             case 'install':
  373.             case 'upgrade':
  374.             case 'upgrade-all':
  375.                 if (isset($data['release_warnings'])) {
  376.                     $this->_displayLine('');
  377.                     $this->_startTable(array(
  378.                         'border' => false,
  379.                         'caption' => 'Release Warnings'
  380.                         ));
  381.                     $this->_tableRow(array($data['release_warnings']), null, array(1 => array('wrap' => 55)));
  382.                     $this->_endTable();
  383.                     $this->_displayLine('');
  384.                 }
  385.                 $this->_displayLine($data['data']);
  386.                 break;
  387.             case 'search':
  388.                 $this->_startTable($data);
  389.                 if (isset($data['headline']) && is_array($data['headline'])) {
  390.                     $this->_tableRow($data['headline'], array('bold' => true), array(1 => array('wrap' => 55)));
  391.                 }
  392.  
  393.                 foreach($data['data'] as $category) {
  394.                     foreach($category as $pkg) {
  395.                         $this->_tableRow($pkg, null, array(1 => array('wrap' => 55)));
  396.                     }
  397.                 };
  398.                 $this->_endTable();
  399.                 break;
  400.             case 'list-all':
  401.                 $this->_startTable($data);
  402.                 if (isset($data['headline']) && is_array($data['headline'])) {
  403.                     $this->_tableRow($data['headline'], array('bold' => true), array(1 => array('wrap' => 55)));
  404.                 }
  405.  
  406.                 foreach($data['data'] as $category) {
  407.                     foreach($category as $pkg) {
  408.                         unset($pkg[3]);
  409.                         unset($pkg[4]);
  410.                         $this->_tableRow($pkg, null, array(1 => array('wrap' => 55)));
  411.                     }
  412.                 };
  413.                 $this->_endTable();
  414.                 break;
  415.             case 'config-show':
  416.                 $data['border'] = false;
  417.                 $opts = array(0 => array('wrap' => 30),
  418.                               1 => array('wrap' => 20),
  419.                               2 => array('wrap' => 35));
  420.                 $this->_startTable($data);
  421.                 if (isset($data['headline']) && is_array($data['headline'])) {
  422.                     $this->_tableRow($data['headline'],
  423.                                      array('bold' => true),
  424.                                      $opts);
  425.                 }
  426.                 foreach($data['data'] as $group) {
  427.                     foreach($group as $value) {
  428.                         if ($value[2] == '') {
  429.                             $value[2] = "<not set>";
  430.                         }
  431.                         $this->_tableRow($value, null, $opts);
  432.                     }
  433.                 }
  434.                 $this->_endTable();
  435.                 break;
  436.             case 'remote-info':
  437.                 $data = array(
  438.                     'caption' => 'Package details:',
  439.                     'border' => false,
  440.                     'data' => array(
  441.                         array("Latest",    $data['stable']),
  442.                         array("Installed", $data['installed']),
  443.                         array("Package",   $data['name']),
  444.                         array("License",   $data['license']),
  445.                         array("Category",  $data['category']),
  446.                         array("Summary",   $data['summary']),
  447.                         array("Description", $data['description']),
  448.                         ),
  449.                     );
  450.             default: {
  451.                 if (is_array($data)) {
  452.                     $this->_startTable($data);
  453.                     $count = count($data['data'][0]);
  454.                     if ($count == 2) {
  455.                         $opts = array(0 => array('wrap' => 25),
  456.                                       1 => array('wrap' => 48)
  457.                         );
  458.                     } elseif ($count == 3) {
  459.                         $opts = array(0 => array('wrap' => 30),
  460.                                       1 => array('wrap' => 20),
  461.                                       2 => array('wrap' => 35)
  462.                         );
  463.                     } else {
  464.                         $opts = null;
  465.                     }
  466.                     if (isset($data['headline']) && is_array($data['headline'])) {
  467.                         $this->_tableRow($data['headline'],
  468.                                          array('bold' => true),
  469.                                          $opts);
  470.                     }
  471.                     foreach($data['data'] as $row) {
  472.                         $this->_tableRow($row, null, $opts);
  473.                     }
  474.                     $this->_endTable();
  475.                 } else {
  476.                     $this->_displayLine($data);
  477.                 }
  478.             }
  479.         }
  480.     }
  481.  
  482.     // }}}
  483.     // {{{ log(text)
  484.  
  485.  
  486.     function log($text, $append_crlf = true)
  487.     {
  488.         if ($append_crlf) {
  489.             return $this->_displayLine($text);
  490.         }
  491.         return $this->_display($text);
  492.     }
  493.  
  494.  
  495.     // }}}
  496.     // {{{ bold($text)
  497.  
  498.     function bold($text)
  499.     {
  500.         if (empty($this->term['bold'])) {
  501.             return strtoupper($text);
  502.         }
  503.         return $this->term['bold'] . $text . $this->term['normal'];
  504.     }
  505.  
  506.     // }}}
  507. }
  508.  
  509. ?>
  510.